﻿using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using WebApi.Attributes;
using NewBee.Core.BusinessService;
using NewBee.Core.Data;

// For more information on enabling Web API for empty projects, visit http://go.microsoft.com/fwlink/?LinkID=397860

namespace WebApi.Controllers
{
    /// <summary>
    /// Test
    /// </summary>
    [Route("api/[controller]/{id?}")]
    public class TestController : BaseController
    {
        AgentFactory _AgentFactory = new AgentFactory();

        /// <summary>
        /// Test分页
        /// </summary>
        /// <param name="PageParam"></param>
        /// <returns></returns>
        [Token]
        [HttpGet,Route("GetPageList")]
        public NewBee.Core.Data.Model.PageResult GetPageList([FromQuery]NewBee.Core.Data.Model.PageParam PageParam)
        {
            NewBee.Core.Data.Model.PageResult _PageResult = new NewBee.Core.Data.Model.PageResult();
            try
            {

                _PageResult = _AgentFactory.Test.GetPageList(PageParam);
            }
            catch (BusinessException ex)
            {
                LogHelper.SetBusinessExceptionLog(ex);
            }
            catch (Exception ex)
            {
                LogHelper.SetExceptionLog(ex);
            }
            return _PageResult;
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="QueryParam"></param>
        /// <returns></returns>
        [Token]
        [HttpGet,Route("GetQueryList")]
        public IEnumerable<NewBee.Core.Models.Test> GetQueryList([FromQuery]NewBee.Core.Data.Model.QueryParam QueryParam)
        {
            IEnumerable<NewBee.Core.Models.Test> queryList = null;
            try
            {
                queryList = _AgentFactory.Test.GetQueryList(QueryParam);
            }
            catch (BusinessException ex)
            {
                LogHelper.SetBusinessExceptionLog(ex);
            }
            catch (Exception ex)
            {
                LogHelper.SetExceptionLog(ex);
            }
            return queryList;
        }

        /// <summary>
        /// 根据主键Id获取数据实体
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [Token]
        [HttpGet]
        public NewBee.Core.Models.Test Get([FromQuery]string Id)
        {
            NewBee.Core.Models.Test model = new NewBee.Core.Models.Test();
            try
            {
                model = _AgentFactory.Test.GetModelById(Id);
            }
            catch (BusinessException ex)
            {
                LogHelper.SetBusinessExceptionLog(ex);
            }
            catch (Exception ex)
            {
                LogHelper.SetExceptionLog(ex);
            }
            return model;
        }


        /// <summary>
        /// 插入
        /// </summary>
        /// <param name="model"></param>
        /// <param name="primaryKey"></param>
        /// <returns></returns>
        [Token]
        [HttpPost]
        public void Post([FromQuery]NewBee.Core.Models.Test model)
        {
            bool result = false;
            try
            {
                result = _AgentFactory.Test.Insert(model);
            }
            catch (BusinessException ex)
            {
                LogHelper.SetBusinessExceptionLog(ex);
            }
            catch (Exception ex)
            {
                LogHelper.SetExceptionLog(ex);
            }
        }


        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="model"></param>
        /// <param name="where"></param>
        /// <param name="primaryKey"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        [Token]
        [HttpPut]
        public void Put([FromQuery]NewBee.Core.Models.Test model)
        {
            bool result = false;
            try
            {
                result = _AgentFactory.Test.Update(model);
            }
            catch (BusinessException ex)
            {
                LogHelper.SetBusinessExceptionLog(ex);
            }
            catch (Exception ex)
            {
                LogHelper.SetExceptionLog(ex);
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="where"></param>
        /// <param name="parameter"></param>
        /// <returns></returns>
        [Token]
        [HttpDelete]
        public void Delete([FromQuery]string Id)
        {
            bool result = false;
            try
            {
                
                result = _AgentFactory.Test.Delete(Id);
            }
            catch (BusinessException ex)
            {
                LogHelper.SetBusinessExceptionLog(ex);
            }
            catch (Exception ex)
            {
                LogHelper.SetExceptionLog(ex);
            }
        }
    }
}
